package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.entity.SysRoleDo;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.service.ISysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 角色信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@RequiredArgsConstructor
@Service
public class SysRoleServiceImpl implements ISysRoleService {
    private final SysRoleMapper sysRoleMapper;

    /**
     * 查询角色信息
     */
    @Override
    public SysRoleDo queryById(Long roleId) {
        return sysRoleMapper.selectById(roleId);
    }

    /**
     * 查询角色信息列表
     */
    @Override
    public TableDataInfo<SysRoleDo> queryPageList(SysRoleDo entity, PageQuery pageQuery) {
        LambdaQueryWrapper<SysRoleDo> lqw = buildQueryWrapper(entity);
        Page<SysRoleDo> result = sysRoleMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询角色信息列表
     */
    @Override
    public List<SysRoleDo> queryList(SysRoleDo entity) {
        LambdaQueryWrapper<SysRoleDo> lqw = buildQueryWrapper(entity);
        return sysRoleMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysRoleDo> buildQueryWrapper(SysRoleDo entity) {
        Map<String, Object> params = entity.getParams();
        LambdaQueryWrapper<SysRoleDo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(entity.getRoleName()), SysRoleDo::getRoleName, entity.getRoleName());
        lqw.eq(StringUtils.isNotBlank(entity.getRoleKey()), SysRoleDo::getRoleKey, entity.getRoleKey());
        lqw.eq(entity.getRoleSort() != null, SysRoleDo::getRoleSort, entity.getRoleSort());
        lqw.eq(StringUtils.isNotBlank(entity.getDataScope()), SysRoleDo::getDataScope, entity.getDataScope());
        lqw.eq(entity.getMenuCheckStrictly() != null, SysRoleDo::getMenuCheckStrictly, entity.getMenuCheckStrictly());
        lqw.eq(entity.getDeptCheckStrictly() != null, SysRoleDo::getDeptCheckStrictly, entity.getDeptCheckStrictly());
        lqw.eq(StringUtils.isNotBlank(entity.getStatus()), SysRoleDo::getStatus, entity.getStatus());
        return lqw;
    }

    /**
     * 新增角色信息
     */
    @Override
    public Boolean insert(SysRoleDo entity) {
        SysRoleDo add = BeanUtil.toBean(entity, SysRoleDo.class);
        validEntityBeforeSave(add);
        Boolean flag = sysRoleMapper.insert(add) > 0;
        if (flag) {
            entity.setRoleId(add.getRoleId());
        }
        return flag;
    }

    /**
     * 修改角色信息
     */
    @Override
    public Boolean updateById(SysRoleDo entity) {
        SysRoleDo update = BeanUtil.toBean(entity, SysRoleDo.class);
        validEntityBeforeSave(update);
        return sysRoleMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysRoleDo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除角色信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return sysRoleMapper.deleteBatchIds(ids) > 0;
    }
}
